<template>
  <div class="home">
    <el-card>
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({
          name: 'advert-create'
        })">添加广告</el-button>
      </div>
      <el-table
        :data="adverts"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="id"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告名称"
          width="180">
        </el-table-column>
        <el-table-column
          label="广告位置">
          <template slot-scope="scope">
            {{ getSpace(scope.row.spaceId) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="广告图片">
          <template slot-scope="scope">
            <el-image
            v-if="scope.row.img"
            style="width: 100px; height: 100px"
            :src="scope.row.img"
            fit="cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="时间">
          <template slot-scope="scope">
            <span>开始时间:{{ getTime(scope.row.startTime) }}</span>
            <br />
            <span>到期时间:{{ getTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          align="center"
          label="上线/下线">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              @change="changeStatus(scope.row)"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button type="info" @click="$router.push({
              name: 'advert-edit',
              params: {
                advertId: scope.row.id
              }
            })">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdList, getAllSpaces, updateSpaceStatus } from '@/services/advert'
import { getTime } from '@/utils/time'

export default Vue.extend({
  name: 'AdvertPage',
  created () {
    this.LoadAllSpaces()
    this.LoadAdList()
  },
  data () {
    return {
      adverts: [],
      form: {
        pageNum: 1,
        pageSize: 5
      },
      allSpaces: []
    }
  },
  methods: {
    async LoadAdList () {
      const { data } = await getAdList(this.form)
      this.adverts = data.content
    },
    async LoadAllSpaces () {
      const { data } = await getAllSpaces()
      this.allSpaces = data.content
    },
    getSpace (id: string) {
      const { name } = this.allSpaces.find((i: any) => i.id === id) as any || {}
      return name
    },
    getTime (time: string | number) {
      return getTime(time)
    },
    async changeStatus (item: any) {
      await updateSpaceStatus({
        id: item.id,
        status: item.status
      })
      // this.$message.success(`广告已${item.status === 0 ? '下线' : '上线'}`)
      item.status === 0 ? this.$message('广告已下线') : this.$message.success('广告已上线')
      this.LoadAdList()
    }
  }
})
</script>

<style lang="scss" scoped></style>
